﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CongThongTin.Entities;

namespace CongThongTin.Administrators
{
    public partial class TheLoaiTinTuc : System.Web.UI.Page
    {
        CongThongTinEntities _entities = new CongThongTinEntities();

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
                hienThiTheLoaiTinTuc();
        }

        void hienThiTheLoaiTinTuc()
        {
            GridView1.DataSource = _entities.THELOAIs;
            GridView1.DataBind();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            hienThiTheLoaiTinTuc();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            hienThiTheLoaiTinTuc();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
            Label lblID = (Label)row.FindControl("lblID_edit");
            TextBox txtTenTheLoai = (TextBox)row.FindControl("txtTenTheLoai");
            int maTheLoai = int.Parse(lblID.Text);

            THELOAI oNewTheLoai = _entities.THELOAIs.SingleOrDefault(t=>t.MATHELOAI== maTheLoai);
            oNewTheLoai.TENTHELOAI = txtTenTheLoai.Text.Trim();

            _entities.SaveChanges();

            GridView1.EditIndex = -1;


            hienThiTheLoaiTinTuc();
        }

        protected void btnThemMoi_Click(object sender, EventArgs e)
        {
            THELOAI oNewTheLoai = new THELOAI();
            oNewTheLoai.TENTHELOAI = tbTenTheLoai.Text.Trim();
            oNewTheLoai.HIENTHI = true;

            _entities.THELOAIs.AddObject(oNewTheLoai);
            _entities.SaveChanges();
            hienThiTheLoaiTinTuc();
            Response.Redirect(Page.Request.Url.ToString(),true);
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            hienThiTheLoaiTinTuc();
        }
      
    }
}